<?php
namespace App\Jobs;

use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Wen\Utils\UserUtils;
use App\Wen\Utils\Utils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use App\Wen\Utils\CollecterUtils;
use Illuminate\Support\Facades\Redis;

class CollecterPublishCommentJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    protected $post_id = 0;
    protected $comment_id = 0;
    protected $comment =  null;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($comment, $post_id, $comment_id)
    {
        //
        $this->comment = $comment;
        $this->post_id = $post_id;
        $this->comment_id = $comment_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if(_empty_($this->comment) || _empty_($this->post_id)){
            return;
        }

        $reply_user_id = $this->comment['reply_unique'] ? CollecterUtils::get_uid_from_unique($this->comment['reply_unique']) : 0;
        if(_empty_($reply_user_id) && $this->comment_id){
            $reply_user_id = WxComment::where('id', $this->comment_id)->value('user_id');
        }
        $user_id = CollecterUtils::get_uid_from_unique($this->comment['author_unique']);
        $comment_content = $this->comment['comment'];
        $comment_img_url = $this->comment['img'];

        $r = WxCommentRepositores::admin_add('', $this->post_id, $this->comment_id,$reply_user_id, $user_id, $comment_content, $comment_img_url);
        if($r){
            Cache::forget('post:commentCount:'.$this->post_id);
            Redis::sadd('realtime:post:set', $this->post_id);
            UserUtils::add_user_experience($user_id, 2);
            $comment_count = WxCommentRepositores::commentCount($this->post_id) ?: 0;//评论数量
            $weight = WxPost::where('id', $this->post_id)->value('weight');
            $weight += ( Utils::getDecayCoefficient($comment_count, 5) * 1000);
            if($weight > time()){
                $weight = time();
            }
            $post_id = $this->post_id;
            WxPost::withoutSyncingToSearch(function () use ($post_id, $weight){
                WxPost::where('id', $post_id)->update(['last_reply_at' => current_time(), 'weight'=>timetostr($weight)]);
            });
            CalculateCommentLikesNumJob::dispatch($r);
            if($this->comment_id && $this->comment_id > 0){
                CalculateCommentLikesNumJob::dispatch($this->comment_id);
            }
            ImitatePostlikesStepJob::dispatch($this->post_id, mini_rand(1, 5))->delay(now()->addMinutes(mini_rand(1, 10)));



            if(_array_key($this->comment, 'subcomments', [])){
                $i = 0;
                $last_time = now();
                $comment_unique_ids = [];

                foreach (array_reverse($this->comment['subcomments']) as $comment){

                    if(!_empty_(_array_key($comment, 'comment_unique', ''))){
                        if(in_array(_array_key($comment, 'comment_unique', ''), $comment_unique_ids)){
                            continue;
                        }else{
                            $comment_unique_ids[] = _array_key($comment, 'comment_unique', '');
                        }
                    }

                    $last_time = $last_time->addMinutes(mini_rand(1, 10) + $i * mini_rand(1, 5) );
                    self::dispatch($comment, $this->post_id, $r)->delay($last_time);
                    $i ++;
                }
            }
        }
    }
}
